// views/index/shop/certificate/paypage/index.js
var BASE_URL = getApp().globalData.BASE_URL
var area = require('../../../../../../utils/area');
var util = require('../../../../../../utils/util');
import Notify from '../../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    area: '', //区域
    userid: '', //用户id
    kuaidi_type: '', //快递下拉菜单
    applyname: '', //申请人姓名
    applyname_pinyin: '', //申请人拼音
    applyphone: '', //申请人手机号
    applywechat: '', //申请人微信号
    attnname: '', //收件人名字
    attnphone: '', //收件人手机号
    attnaddress: '', //收件人地址
    attnemail: '', //收件人邮箱
    schoolname: '', //学校名字
    majorname: '', //专业名字
    remarks: "", //备注
    last_price: '', //最后总价
    shoppingcardata: '', //购物车数据
    message: '',
    id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    wx.request({
      url: `https://liudediy.com/gongzheng/dizhione`,
      method: "POST",
      data: {
        id: options.id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        _this.setData({
          id: options.id,
          applyname: res.data.sname, //申请人姓名
          applyname_pinyin: res.data.pingyin, //申请人拼音
          applyphone: res.data.tel, //申请人手机号
          applywechat: res.data.wechat, //申请人微信号
          attnname: res.data.name, //收件人名字
          attnphone: res.data.dianhua, //收件人手机号
          attnaddress: res.data.dizhi, //收件人地址
          attnemail: res.data.email, //收件人邮箱
          schoolname: res.data.waiyu, //学校名字
          majorname: res.data.zhuanye, //专业名字
          remarks: res.data.beizhu, //备注
          area: res.data.qvyu,
          last_price: res.data.zong,
          kuaidi_type: res.data.kdname,
          message: res.data
        })
      }
    })
  },


  onShow: function () {
    var _this = this
    if (util.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        userid: userinfo.id
      })
      _this.getshoppingdata()
    }
  },



  //获取购物车数据
  getshoppingdata: function () {
    var _this = this
    wx.request({
      url: `${BASE_URL}/gongzheng/gouwuche_list`,
      method: "POST",
      data: {
        pid: _this.data.userid,
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        if (res.data.status == 0) {
          Dialog.alert({
            title: '提示',
            message: '购物车为空,请添加商品',
          }).then(() => {
            wx.navigateBack({
              delta: 1
            })
          });
        }
        var str = []
        if (res.data.status !== 0) {
          res.data.info.forEach((item, index) => {
            str.push(item.subtotal)
          })
          var arr = str.reduce(function (a, b) {
            return a + b;
          });
          if (res.data.jiazuo !== 0) {
            arr = arr + 60
            _this.setData({
              jz: 60
            })
          } else {
            _this.setData({
              jz: 0
            })
          }
          if (_this.data.jiajibtn) {
            arr = arr + 120 * res.data.num
          }
          if (_this.data.saomiaobth) {
            arr = arr + 30
          }
          if (_this.data.kuaidi_type == '顺丰标快(23元)') {
            arr = arr + 23
          }
          if (_this.data.kuaidi_type == '顺丰特惠(18元)') {
            arr = arr + 18
          }
          _this.setData({
            shoppingcardata: res.data,
          })
        } else {
          _this.setData({
            shoppingcardata: '',
            last_price: 0
          })
        }

      }
    })

  },



  first_next_one_bu: function () {
    var _this = this
    console.log(_this.data.id, _this.data.userid)
    //提交下单
    wx.request({
      url: `${BASE_URL}/gongzheng/check`,
      method: "POST",
      data: {
        id: _this.data.id,
        type: 'xiaochengxv',
        uid: _this.data.userid
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        wx.navigateTo({
          url: `../third/index?id=${res.data}`
        })
      }
    })
  }
})